﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.DAL;
using Register.Command;
using Register.BLL;
using System.Web.Script.Serialization;
namespace RegisterWeb.Manager.HospitalManager.ashx
{
    /// <summary>
    /// HospitalInstallHandler 的摘要说明
    /// </summary>
    public class HospitalInstallHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            #region 添加配置
            if (action.Equals("addHospitalInstall"))
            {
                HospitalInstall hospitalInstall = new HospitalInstall();

                hospitalInstall.HospitalInstall_ID = DataIDHelper.GetDataID("HospitalInstall_ID");

                hospitalInstall.Hospital_ID= context.Request["id"];
                hospitalInstall.HospitalInstall_AddTime = DateTime.Now;
                hospitalInstall.HospitalInstall_Recoder = "doublez";  //临时赋值
                hospitalInstall.HosptialInstall_BeforeDay =Convert.ToInt32(context.Request["HosptialInstall_BeforeDay"]);
                hospitalInstall.HosptialInstall_StartRegistrationTime =Convert.ToDateTime(context.Request["HosptialInstall_StartRegistrationTime"]);
                hospitalInstall.HosptiallInstall_AppointmentPhone = context.Request["HosptiallInstall_AppointmentPhone"];
                string workTime = context.Request["HosptiallInstall_WorkTime"] + "~" + context.Request["HosptiallInstall_WorkTime1"];
                hospitalInstall.HosptiallInstall_WorkTime = workTime;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(HospitalInstallBLL.AddHospitalInstall(hospitalInstall), "医院配置成功！")));

            }
            #endregion
            #region 获取配置信息
            else if (action.Equals("getHospitalInstall"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<HospitalInstall> list = new List<HospitalInstall>();
                list = HospitalInstallBLL.GetPagedHospitalInstall(row, page, " 1=1 ");

                List<HospitalInstallJSON> json = new List<HospitalInstallJSON>();
                foreach (HospitalInstall hospitalInstall in list)
                { //对于每一个医院对象，根据获得的城市ID获得城市名，并绑定


                    json.Add(new HospitalInstallJSON(HospitalInstallBLL.GetHospitalNameById(hospitalInstall.Hospital_ID), Convert.ToString(hospitalInstall.HospitalInstall_AddTime),hospitalInstall.HospitalInstall_ID, hospitalInstall.HospitalInstall_Recoder, Convert.ToString(hospitalInstall.HosptialInstall_BeforeDay), Convert.ToString(hospitalInstall.HosptialInstall_StartRegistrationTime), hospitalInstall.HosptiallInstall_AppointmentPhone,hospitalInstall.HosptiallInstall_WorkTime));
                }

                HospitalInstallListJSON hospitalInstallListJSON = new HospitalInstallListJSON();
                hospitalInstallListJSON.total = HospitalInstallBLL.GetHospitalInstallCount(null);
                hospitalInstallListJSON.rows = json;

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(hospitalInstallListJSON));

            }
            #endregion
            #region 删除医院配置
            else if (action.Equals("deleteHospitalInstall"))
            {
                String ID = context.Request["id"];
                if (HospitalInstallBLL.DeleteHospitalInstall(ID))
                    context.Response.Write("ok");
                else
                    context.Response.Write("no");
            }
            #endregion
            #region 更新医院配置
            else if (action.Equals("editHospitalInstall")) {

                HospitalInstall hospitalInstall = new HospitalInstall();
                hospitalInstall.HospitalInstall_ID= context.Request["id"];
                hospitalInstall.HospitalInstall_AddTime = DateTime.Now;
                hospitalInstall.HospitalInstall_Recoder = "doublez";  //临时赋值
                hospitalInstall.HosptialInstall_BeforeDay = Convert.ToInt32(context.Request["HosptialInstall_BeforeDay"]);
                hospitalInstall.HosptialInstall_StartRegistrationTime = Convert.ToDateTime(context.Request["HosptialInstall_StartRegistrationTime"]);
                hospitalInstall.HosptiallInstall_AppointmentPhone = context.Request["HosptiallInstall_AppointmentPhone"];
                string workTime = context.Request["HosptiallInstall_WorkTime"] + "~" + context.Request["HosptiallInstall_WorkTime1"];
                hospitalInstall.HosptiallInstall_WorkTime = workTime;
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(HospitalInstallBLL.UpdateHospitalInstall(hospitalInstall), "配置编辑成功！")));
            }
            #endregion

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
class message3
{
    public message3(bool success, string msg)
    {
        this.msg = msg;
        this.success = success;
    }
    public bool success { get; set; }
    public string msg { get; set; }
}
class HospitalInstallListJSON
{

    public int total { get; set; }
    public List<HospitalInstallJSON> rows { get; set; }

}

class HospitalInstallJSON
{
    public string HospitalInstall_ID
    {
        set;
        get;
    }
    public string Hospital_ID
    {
        get;
        set;
    }
    public string HosptialInstall_StartRegistrationTime
    {
        get;
        set;
    }

    public string HosptialInstall_BeforeDay
    {
        get;
        set;
    }

    public string HosptiallInstall_WorkTime
    {
        get;
        set;
    }

    public string HosptiallInstall_AppointmentPhone
    {
        get;
        set;
    }
    public string HospitalInstall_AddTime
    {
        get;
        set;
    }

    public string HospitalInstall_Recoder
    {
        get;
        set;
    }

    public HospitalInstallJSON(String Hospital_ID, String HospitalInstall_AddTime, String HospitalInstall_ID, String HospitalInstall_Recoder, String HosptialInstall_BeforeDay, String HosptialInstall_StartRegistrationTime, String HosptiallInstall_AppointmentPhone, String HosptiallInstall_WorkTime)
    {
        this.Hospital_ID = Hospital_ID;
        this.HospitalInstall_AddTime = HospitalInstall_AddTime;
        this.HospitalInstall_ID = HospitalInstall_ID;
        this.HospitalInstall_Recoder = HospitalInstall_Recoder;
        this.HosptialInstall_BeforeDay = HosptialInstall_BeforeDay;
        this.HosptialInstall_StartRegistrationTime = HosptialInstall_StartRegistrationTime;
        this.HosptiallInstall_AppointmentPhone = HosptiallInstall_AppointmentPhone;
        this.HosptiallInstall_WorkTime = HosptiallInstall_WorkTime;
    }

}
